<template>
    <div class="mail" :style="'min-height:' + screeHeight + 'px;'">
        <!-- <top-bar title="通讯录"></top-bar> -->
        <van-search v-model="search" @search="handelSearch" placeholder="请输入酒店或用户" />
        <div class="list-wrapper" v-if="searchList.length || searchHList.length">
            <div v-for="item in searchHList" :key="item.id">
                <div v-if="item.id != 2" class="list-item" @click="goMailDetail(item)">
                    <img class="item-icon" src="@/assets/icon_01.png" alt="" />
                    <p class="item-title">{{ item.cn_title }}</p>
                </div>
            </div>
        </div>
        <div v-if="searchList.length || searchHList.length" class="people-list-wrapper">
            <div v-for="user in searchList" :key="user.id" class="people-item" @click="goUserInfo(user)">
                <img class="item-icon" :src="user.avatar" alt="" />
                <div class="item-info">
                    <p class="item-name">{{ user.name }}</p>
                    <p class="item-job">职位:{{ user.eu_job }}</p>
                </div>
            </div>
        </div>
        <div class="list-wrapper" v-else>
            <div v-for="item in list" :key="item.id">
                <div v-if="item.id != 2" class="list-item" @click="goMailDetail(item)">
                    <img class="item-icon" src="@/assets/icon_01.png" alt="" />
                    <p class="item-title">{{ item.cn_title }}</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { Dialog, Toast } from "vant";
import TopBar from "@/components/topBar.vue";
export default {
    name: "mail",
    data() {
        return {
            search: "",
            list: [],
            searchList: [],
            searchHList: [],
            screeHeight: window.innerHeight - 220,
        };
    },
    mounted() {
        this.departments();
    },
    methods: {
        handelSearch(val) {
            if (!val) {
                this.searchList = [];
                this.searchHList = [];
            } else {
                Toast.loading({
                    message: "搜索中...",
                    forbidClick: true,
                    duration: 0,
                });
                this.$api.member({ key: val }).then((res) => {
                    Toast.clear();
                    console.log(res);
                    if (res.status_code == 200) {
                        this.searchList = res.data.lists1;
                        this.searchHList = res.data.lists2;
                    }
                });
            }
        },
        goMailDetail(val) {
            this.$router.push({
                path: `/mailDetail/${val.id}`,
            });
        },
        goUserInfo(val) {
            this.$router.push({
                path: "/userInfo",
                query: {
                    user: JSON.stringify(val),
                },
            });
        },
        departments() {
            if (!localStorage.getItem("_Agreed_")) {
                Dialog.alert({
                    // messageAlign:'left',
                    confirmButtonColor: "#224448",
                    title: "加入须知",
                    message:
                        "该应用旨在帮助酒店更好的追踪LeadShare，应用通讯录页面将展示姓名、手机号、邮箱，作为即时消息的工具。请不要分享宾客、客户、员工的机密信息。使用期间请严格遵守大中华区个人使用社交媒体指南及企业微信使用准则。",
                }).then(() => {
                    localStorage.setItem("_Agreed_", true);
                });
            }
            this.$api.departments().then((res) => {
                if (res.status_code == 200) {
                    this.list = res.data.department_data;
                } else {
                    Dialog({ message: res.message });
                }
            });
        },
    },
    components: {
        TopBar,
    },
};
</script>

<style lang="scss">
.mail {
    .van-search__content {
        background-color: #f4f4f4;
    }

    .van-search {
        padding: 18px 15px;

        .van-cell {
            padding: 8px 8px 8px 0;
        }
    }
}

//超过750px时，修改字体
@media screen and (min-width: 750px) {
    .mail {
        .people-list-wrapper {
            .people-item {
                .item-info {
                    .item-name {
                        font-size: 20px !important;
                    }
                    .item-job {
                        font-size: 18px !important;
                    }
                }
            }
        }

        .list-wrapper {
            .list-item {
                .item-title {
                    font-size: 20px !important;
                }
            }
        }
    }
}
</style>
<style lang="scss" scoped>
@supports (bottom: env(safe-area-inset-bottom)) {
    .mail {
        height: calc(100vh - 50px - constant(safe-area-inset-bottom)) !important;
        height: calc(100vh - 50px - env(safe-area-inset-bottom)) !important;
    }
}

.mail {
    position: relative;
    z-index: 2;
    height: calc(100vh - 50px);
    background-color: #fff;
    overflow-y: auto;

    .people-list-wrapper {
        .people-item {
            margin: 0 15px;
            display: flex;
            align-items: center;
            padding: 20px 0;
            border-bottom: 1px solid #f4f4f4;

            img.item-icon {
                width: 40px;
                height: 40px;
                margin-right: 20px;
                border-radius: 10px;
            }

            .item-info {
                line-height: 1;

                .item-name {
                    font-size: 14px;
                    font-weight: bold;
                    color: #333333;
                    margin-bottom: 14px;
                }

                .item-job {
                    font-size: 13px;
                    color: #666666;
                }
            }
        }
    }

    .list-wrapper {
        padding: 0 15px;

        .list-item {
            display: flex;
            align-items: center;
            padding: 20px 0;
            border-bottom: 1px solid #f4f4f4;

            .item-icon {
                width: 40px;
                height: 40px;
                margin-right: 20px;
            }

            .item-title {
                font-size: 14px;
                font-weight: bold;
                color: #333333;
            }
        }
    }
}
</style>
